//
//  ViewController.m
//  25-多线程
//
//  Created by Snow on 16/8/17.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //NSThread
    //ios下的多线程
    //1.进程和线程 
    //什么是进程 ? 进程是当用户下达执行应用程序的命令时, 所启动的东西.进程比作工厂.
    //什么是线程 ? 线程是进程的执行单位.线程必做工人.
    //2.多线程
    //为了更好地进行一些程序的操作.节省时间, 提高效率.
    //一个进程不能过多的创建线程.资源消耗太大(工厂不能有太多工人, 工资发不起) 开辟5个左右的线程
    //所有UI的操作 都放在主线程中进行
    //需要开辟线程的耗时操作, 图像文字渲染, 大数据存储, 网络请求
    //3.NSThead
    //1>获取当前线程      [NSThread currentThread]
    // <NSThread: 0x600000064e00>{number = 1, name = main}
    //name 线程名 number 编号 main代表主线程
    NSLog(@"%@", [NSThread currentThread]);
    //2>开辟子线程 执行操作 子线程随机操作
    [NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];
    [NSThread detachNewThreadWithBlock:^{
        NSLog(@"%@", [NSThread currentThread]);
    }];
    //3> 延迟当前线程的执行, 固定日期
//    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
//    [NSThread sleepUntilDate:date];
//    NSLog(@"111111");
    //4> 相对时间
    [NSThread sleepForTimeInterval:0];
    //5> exit 退出当前线程 不要退出主线程 退出后 线程后续操作不会再执行
//    [NSThread exit];
    NSLog(@"%@", [NSThread mainThread]);
    //6> 线程优先级 threadPriority
    //默认线程优先级 为 0.5
    double priority = [NSThread threadPriority];
    NSLog(@"%lf", priority);
    //7>. name 线程名
    [NSThread mainThread].name = @"肖扬的线程";
    NSLog(@"%@", [NSThread currentThread]);
    //8>.判断是否是主线程
    [NSThread isMainThread];//判断当前线程是不是主线程
    [[NSThread mainThread] isMainThread];//判断线程对象是不是主线程
    //9>.初始化方法 需要手动管理线程的生命周期
    //初始化一个未启动的线程
    NSThread *newThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThread) object:nil];
    //练习2 下载图片
    [newThread start];
    //10. 将耗时的操作 放入后台执行 不关心线程问题
    [self performSelectorInBackground:@selector(newThread) withObject:nil];
    //11.返回主线程 performSelectorInBackground
    
}
- (void)myThread
{
    NSLog(@"%s", __FUNCTION__);
}
- (void)newThread
{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http:pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg"]];
    //返回主线程, 显示图片
    [self performSelectorOnMainThread:@selector(onmainthreadhandleImage:) withObject:data waitUntilDone:YES];
    NSThread *thread = [NSThread currentThread];
    thread.name = @"王道林的线程";
    NSLog(@"newThread%@---%lf", thread, [NSThread threadPriority]);
}
- (void)onmainthreadhandleImage:(NSData *)data
{
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    image.image = [UIImage imageWithData:data];
    [self.view addSubview:image];

}
- (IBAction)clicked:(id)sender {
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
